home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianAxes.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  238 lines

  1. /*ScianAxes.c
  2.   Eric Pepke
  3.   21 February 1993
  4.   Stuff for axes within SciAn
  5.  
  6.   By default, SciAn only knows about axes X, Y, and Z.  The file .scianAxes
  7.   in the home directory contains additional axes that SciAn should know 
  8.   about.  It is a text file containing several lines, all of the form
  9.  
  10.   axisName = otherAxisName
  11.  
  12.   where axisName is an axis to define and otherAxisName is an existing axis.
  13.   For example,
  14.  
  15.   longitude = x
  16.  
  17.   Axis names are not sensitive to case.
  18. */
  19.  
  20. #include "Scian.h"
  21. #include "ScianTypes.h"
  22. #include "ScianIDs.h"
  23. #include "ScianWindows.h"
  24. #include "ScianAxes.h"
  25. #include "ScianTextFiles.h"
  26. #include "ScianScripts.h"
  27.  
  28. AxisPtr axes;
  29.  
  30. #ifdef PROTO
  31. AxisPtr FindNamedAxis(AxisPtr root, char *name)
  32. #else
  33. AxisPtr FindNamedAxis(root, name)
  34. AxisPtr root;
  35. char *name;
  36. #endif
  37. /*Finds the named axis in tree at root, returning it or returning nothing*/
  38. {
  39.     if (root)
  40.     {
  41.     int result;
  42.  
  43.     result = strcmp2(name, root -> name);
  44.     if (result < 0)
  45.     {
  46.         /*To the left*/
  47.         return FindNamedAxis(root -> left, name);
  48.     }
  49.     else if (result > 0)
  50.     {
  51.         /*To the right*/
  52.         return FindNamedAxis(root -> right, name);
  53.     }
  54.     else
  55.     {
  56.         /*Spot on*/
  57.         return root;
  58.     }
  59.     }
  60.     else
  61.     {
  62.     return (AxisPtr) NULL;
  63.     }
  64. }
  65.  
  66. #ifdef PROTO
  67. static AxisPtr AddAxisHelper(AxisPtr *rootPtr, char *name, int dimension)
  68. #else
  69. static AxisPtr AddAxisHelper(rootPtr, name, dimension)
  70. AxisPtr *rootPtr;
  71. char *name;
  72. int dimension;
  73. #endif
  74. /*Recursive helper for AddAxis*/
  75. {
  76.     if (*rootPtr)
  77.     {
  78.     int comparison;
  79.  
  80.     comparison = strcmp2(name, (*rootPtr) -> name);
  81.     if (comparison < 0)
  82.     {
  83.         /*To the left*/
  84.         return AddAxisHelper(&((*rootPtr) -> left), name, dimension);
  85.     }
  86.     else if (comparison > 0)
  87.     {
  88.         /*To the right*/
  89.         return AddAxisHelper(&((*rootPtr) -> right), name, dimension);
  90.     }
  91.     else
  92.     {
  93.         /*Spot on, just change dimension*/
  94.         (*rootPtr) -> dimension = dimension;
  95.         return *rootPtr;
  96.     }
  97.     }
  98.     else
  99.     {
  100.     /*Needs a new node here*/
  101.     (*rootPtr) = newp(Axis);
  102.     (*rootPtr) -> left = (*rootPtr) -> right = (AxisPtr) NULL;
  103.     (*rootPtr) -> dimension = dimension;
  104.     (*rootPtr) -> name = Alloc(strlen(name) + 1);
  105.     strcpy((*rootPtr) -> name, name);
  106.     return *rootPtr;
  107.     }
  108. }
  109.  
  110. #ifdef PROTO
  111. AxisPtr AddAxis(char *name, int dimension)
  112. #else
  113. AxisPtr AddAxis(name, dimension)
  114. #endif
  115. /*Adds an axis to the axis system.  Only updates dimension if it's there.*/
  116. {
  117.     return AddAxisHelper(&axes, name, dimension);
  118. }
  119.  
  120. #ifdef PROTO
  121. int FindAxisDimension(char *name)
  122. #else
  123. int FindAxisDimension(name)
  124. char *name;
  125. #endif
  126. /*Returns the dimension of axis name or -1 if not found*/
  127. {
  128.     AxisPtr axis;
  129.  
  130.     axis = FindNamedAxis(axes, name);
  131.     if (axis)
  132.     {
  133.     return axis -> dimension;
  134.     }
  135.     else
  136.     {
  137.     return -1;
  138.     }
  139. }
  140.  
  141. #ifdef PROTO
  142. void DeleteAxes(AxisPtr axes)
  143. #else
  144. void DeleteAxes(axes)
  145. AxisPtr axes;
  146. #endif
  147. /*Deletes tree of axes*/
  148. {
  149.     if (axes)
  150.     {
  151.     DeleteAxes(axes -> left);
  152.     DeleteAxes(axes -> right);
  153.     SAFEFREE(axes -> name);
  154.     Free(axes);
  155.     }
  156. }
  157.  
  158. #ifdef PROTO
  159. void InitAxes(void)
  160. #else
  161. void InitAxes()
  162. #endif
  163. {
  164.     TextFilePtr axesFile;
  165.     int curLine;
  166.     char *tempStr;
  167.     char *s;
  168.     char newAxisName[TEMPSTRSIZE], oldAxisName[TEMPSTRSIZE];
  169.     char fileName[TEMPSTRSIZE];
  170.  
  171.     axes = 0;
  172.     AddAxis("X", 0);
  173.     AddAxis("Y", 1);
  174.     AddAxis("Z", 2);
  175.  
  176.     /*Read the axes file*/
  177.     sprintf(fileName, "%s/%s", getenv("HOME"), ".scianAxes");
  178.     curLine = 0;
  179.     axesFile = OpenTextFile(fileName, TF_READ + TF_CONTINUATION + TF_HASHCOMMENTS);
  180.     if (axesFile)
  181.     {
  182.     while (tempStr = GetNextLine(axesFile))
  183.     {
  184.         ++curLine;
  185.         s = tempStr;
  186.         SKIPBLANKS(s);
  187.         if (*s && *s != '#' && *s != '\n')
  188.         {
  189.         SHIFTKW(newAxisName, s);
  190.         SKIPBLANKS(s);
  191.         if (*s != '=')
  192.         {
  193.             CurLineError(axesFile, 
  194.             "An equals (=) is expected after the first axis name.",
  195.             NULL, NULL);
  196.         }
  197.         else
  198.         {
  199.             int dimension;
  200.  
  201.             ++s;
  202.             SHIFTKW(oldAxisName, s);
  203.  
  204.             dimension = FindAxisDimension(oldAxisName);
  205.             if (dimension >= 0)
  206.             {
  207.             AddAxis(newAxisName, dimension);
  208.             }
  209.             else
  210.             {
  211.             CurLineError(axesFile, 
  212.             "The axis on the right has not been defined.",
  213.             NULL, NULL);
  214.             }
  215.  
  216.             SKIPBLANKS(s);
  217.             if (*s && *s != '\n')
  218.             {
  219.             CurLineError(axesFile, 
  220.             "Extraneous stuff at the end of line is ignored.",
  221.             NULL, NULL);
  222.             }
  223.         }
  224.         }
  225.     }
  226.     CloseTextFile(axesFile);
  227.     }
  228. }
  229.  
  230. #ifdef PROTO
  231. void KillAxes(void)
  232. #else
  233. void KillAxes()
  234. #endif
  235. {
  236.     DeleteAxes(axes);
  237. }
  238.